package com.exam.controller;

import com.exam.entity.*;
import com.exam.serviceimpl.LoginServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {

    @Autowired
    private LoginServiceImpl loginService;

    @PostMapping("/login")
    public ApiResult login(@RequestBody Login login) {

        String username = login.getUsername();
        String password = login.getPassword();

        String phone = login.getPhone();
        User user;
        if(phone != null){
            user = loginService.userLoginByPhone(phone);
        }else{
            user = loginService.userLogin(username, password);
        }

        if (user != null) {
            return ApiResultHandler.buildApiResult(200, "请求成功", user);
        }
//        Teacher teacherRes = loginService.teacherLogin(username,password);
//        if (teacherRes != null) {
//            return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
//        }
//
//        Student studentRes = loginService.studentLogin(username,password);
//        if (studentRes != null) {
//            return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
//        }
        return ApiResultHandler.buildApiResult(400, "请求失败", null);
    }


}
